package common

import (
	"common/mildware"
	"common/models"
	"common/validate"
	"github.com/gin-gonic/gin"
	"github.com/go-playground/validator/v10"
	"go.uber.org/zap"
)

// AddVideoType 添加分类
// @Summary 添加分类
// @Description 添加分类
// @Tags 通用
// @Accept application/json
// @Produce application/json
// @Param object body docModel.DocAddVideoTypeReq false "参数"
// @Security ApiKeyAuth
// @Success 200 {object} docModel.DocResp
// @Router /common/video/type/add [post]
func (h CHandler) AddVideoType(c *gin.Context) {
	resp := new(models.RespData)
	req := new(models.AddVideoTypeReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Common:AddVideoType with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Common:AddVideoType with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//获得token
	ID, _, nickName, err := mildware.GetCyrrentUserID(c)
	if err != nil {
		zap.L().Error("Common: AddVideoType mildware.GetCyrrentUserID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	videoType := &models.VideoTypeInfo{
		Name:         nickName,
		OperatorId:   ID,
		OperatorName: nickName,
	}
	if err := h.M.TypeVideoAdd(videoType); err != nil {
		zap.L().Error("Common: AddVideoType h.M.TypeVideoAdd err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	resp.SendSuccess(c)
}

// UpdateVideoType 更新分类
// @Summary 更新分类
// @Description 更新分类
// @Tags 通用
// @Accept application/json
// @Produce application/json
// @Param object body docModel.DocUpdateVideoTypeReq false "参数"
// @Security ApiKeyAuth
// @Success 200 {object} docModel.DocResp
// @Router /common/video/type/update [post]
func (h CHandler) UpdateVideoType(c *gin.Context) {
	resp := new(models.RespData)
	req := new(models.UpdateVideoTypeReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Common:UpdateVideoType with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Common:UpdateVideoType with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	//获得token
	ID, _, nickName, err := mildware.GetCyrrentUserID(c)
	if err != nil {
		zap.L().Error("Common: UpdateVideoType mildware.GetCyrrentUserID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	//查找
	videoTypeInfo, err := h.M.TypeVideoQueryByID(req.TypeID)
	if err != nil {
		zap.L().Error("Common: UpdateVideoType  h.M.TypeVideoQueryByID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	videoTypeInfo.OperatorId = ID
	videoTypeInfo.OperatorName = nickName
	videoTypeInfo.Name = req.TypeName
	if err := h.M.TypeVideoUpdateByID(videoTypeInfo); err != nil {
		zap.L().Error("Common: UpdateVideoType.M.TypeVideoUpdateByID err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	resp.SendSuccess(c)
}

// DeleteVideoType 删除分类
// @Summary 删除分类
// @Description 删除分类
// @Tags 通用
// @Accept application/json
// @Produce application/json
// @Param object body docModel.DocDeleteVideoTypeReq false "参数"
// @Security ApiKeyAuth
// @Success 200 {object} docModel.DocResp
// @Router /common/video/type/delete [post]
func (h CHandler) DeleteVideoType(c *gin.Context) {
	resp := new(models.RespData)
	req := new(models.DeleteVideoTypeReq)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Common:DeleteVideoType with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Common:DeleteVideoType with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	if err := h.M.TypeVideoDeleteTrueBy(req.TypeID); err != nil {
		zap.L().Error("Common: DeleteVideoType h.M.TypeVideoDeleteTrueBy err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	resp.SendSuccess(c)
}

// GetAllVideoType 获得分类all
// @Summary 获得分类all
// @Description 获得分类all
// @Tags 通用
// @Accept application/json
// @Produce application/json
// @Param object body docModel.DocSearchAllTypeInfo false "参数"
// @Security ApiKeyAuth
// @Success 200 {object} docModel.DocResp
// @Router /common/video/type/all [post]
func (h CHandler) GetAllVideoType(c *gin.Context) {
	resp := new(models.RespData)
	req := new(models.SearchAllTypeInfo)
	if err := c.ShouldBindJSON(req); err != nil {
		errs, ok := err.(validator.ValidationErrors)
		if !ok {
			zap.L().Error("Common:GetAllVideoType with invalid fail", zap.Error(err))
			resp.SendServerBusy(c)
			return
		} else {
			zap.L().Error("Common:GetAllVideoType with invalid fail", zap.Error(errs))
			resp.SendCodeWithMsg(c, models.CodeInvalidParam, validate.RemoveTopStruct(errs.Translate(validate.Trans)))
			return
		}
	}
	list, total, err := h.M.TypeVideoSearchAll(req.Page, req.Size)
	if err != nil {
		zap.L().Error("Common: DeleteVideoType h.M.TypeVideoDeleteTrueBy err", zap.Error(err))
		resp.SendServerBusy(c)
		return
	}
	resp.Data = list
	resp.Count = total
	resp.SendSuccess(c)
}
